<?php

namespace app\service;

//玩家角色服务层

use app\model\Staff;
use app\model\UserStaff;

class UserStaffService extends BaseService
{

    //添加玩家角色
    public function add_user_staff($user_id, $staff_id): array
    {
        //查询角色是否存在
        $staff = Staff::where("id", $staff_id)->find();
        if (!$staff) {
            return $this->returnFail("角色不存在");
        }

        //查询玩家是否已经拥有该角色
        $user_staff = UserStaff::where("user_id", $user_id)->where("staff_id", $staff_id)->find();
        if ($user_staff) {
            return $this->returnFail("玩家已经拥有该角色");
        }

        //添加玩家角色
        $user_staff = new UserStaff();
        $user_staff->user_id = $user_id;
        $user_staff->staff_id = $staff_id;
        $user_staff->lv = 1;
        $user_staff->exp = 0;
        $user_staff->save();

        return $this->returnSuccess("添加成功");
    }

    //查询是否拥有角色
    public function has_staff($user_id, $staff_id): bool
    {
        $user_staff = UserStaff::where("user_id", $user_id)->where("staff_id", $staff_id)->find();
        if ($user_staff) {
            return true;
        }
        return false;
    }




}